﻿Imports System.Data.SqlClient
Public Class MovimientosDAO
    Inherits Conexion
    Dim cmd As New SqlCommand
    'Funciones


    Public Function mostrar(ByVal reg As Movimientos) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("Movimientos_mostrar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@id", reg.pID_T_Documento)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function

    Public Function inventario_mostrar_por_bodega(ByVal reg As Movimientos) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("inventario_mostrar_por_bodega")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@id", reg.pBodega)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function


    Public Function mostrar_codig(ByVal reg As Movimientos) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("movimiento_codigo")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@idMov", reg.pID_Movimiento)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function


    Public Function mostrarUltimoRegistro() As DataTable
        Try
            conectar()
            cmd = New SqlCommand("movimiento_reciente")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            Dim dr As SqlDataReader
            cmd.ExecuteNonQuery()
            dr = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(dr)
            Dim str_Rol As String = dt.Rows(0).Item(0).ToString
            registroUltimo = str_Rol
            Return Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrarUltimoRegistro1() As DataTable
        Try
            conectar()
            cmd = New SqlCommand("movimiento_reciente")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            Dim dr As SqlDataReader
            cmd.ExecuteNonQuery()
            dr = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(dr)
            Dim str_Rol As String = dt.Rows(0).Item(0).ToString
            registroUltimo1 = str_Rol
            Return Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrarUltimoRegistro2() As DataTable
        Try
            conectar()
            cmd = New SqlCommand("movimiento_reciente")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            Dim dr As SqlDataReader
            cmd.ExecuteNonQuery()
            dr = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(dr)
            Dim str_Rol As String = dt.Rows(0).Item(0).ToString
            registroUltimo2 = str_Rol
            Return Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function

    Public Function mostrarDetalle_Entradas(ByVal reg As Movimientos) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("detalle_mostrar_entradas")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@id", reg.pID_Movimiento)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrarDetalle_Salidas(ByVal reg As Movimientos) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("detalle_mostrar_salidas")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@id", reg.pID_Movimiento)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrarFiltro_Entradas(ByVal reg As Items) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("filtro_producto_entradas")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nomb", reg.pNC)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function


    Public Function mostrarFiltro_Facturacion(ByVal reg As Items) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("Inventario_Factura")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nomb", reg.pNC)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function


    Public Function mostrarFiltro_Salidas(ByVal reg As Items) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("filtro_producto_salidas")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nomb", reg.pNC)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function agregar(ByVal reg As Movimientos) As Boolean
        Try
            conectar()
            cmd = New SqlCommand("Movimientos_agregar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            'Parametros
            cmd.Parameters.AddWithValue("@fec", reg.pFehca)
            cmd.Parameters.AddWithValue("@desc", reg.pDescripcion)
            cmd.Parameters.AddWithValue("@stotal", reg.pSubTotal)
            cmd.Parameters.AddWithValue("@iva", reg.pIva)
            cmd.Parameters.AddWithValue("@total", reg.pTotal)
            cmd.Parameters.AddWithValue("@idTdoc", reg.pID_T_Documento)
            cmd.Parameters.AddWithValue("@idBodega", reg.pBodega)
            cmd.Parameters.AddWithValue("@idSocio", reg.pSocio)
            cmd.Parameters.AddWithValue("@accion", SqlDbType.Bit)
            cmd.Parameters("@accion").Direction = ParameterDirection.Output
            cmd.ExecuteNonQuery()
            Return cmd.Parameters("@accion").Value
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
            Return False
        Finally
            desconectar()
        End Try
    End Function

    Public Function actualizar(ByVal reg As Movimientos) As Boolean
        Try
            conectar()
            cmd = New SqlCommand("Movimiento_actualizar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            'Parametros
            cmd.Parameters.AddWithValue("@id", reg.pID_Movimiento)
            cmd.Parameters.AddWithValue("@se", reg.pCodigo)
            cmd.Parameters.AddWithValue("@fec", reg.pFehca)
            cmd.Parameters.AddWithValue("@desc", reg.pDescripcion)
            cmd.Parameters.AddWithValue("@stotal", reg.pSubTotal)
            cmd.Parameters.AddWithValue("@iva", reg.pIva)
            cmd.Parameters.AddWithValue("@total", reg.pTotal)
            cmd.Parameters.AddWithValue("@idTdoc", reg.pID_T_Documento)
            cmd.Parameters.AddWithValue("@idBodega", reg.pBodega)
            cmd.Parameters.AddWithValue("@idSocio", reg.pSocio)
            cmd.Parameters.AddWithValue("@accion", SqlDbType.Bit)
            cmd.Parameters("@accion").Direction = ParameterDirection.Output
            cmd.ExecuteNonQuery()
            Return cmd.Parameters("@accion").Value
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
            Return False
        Finally
            desconectar()
        End Try
    End Function

End Class
